//package me.gavin.net
//
//import android.view.View
//import androidx.appcompat.widget.Toolbar
//import com.uber.autodispose.CompletableSubscribeProxy
//import com.uber.autodispose.FlowableSubscribeProxy
//import com.uber.autodispose.ObservableSubscribeProxy
//import com.uber.autodispose.SingleSubscribeProxy
//import io.reactivex.Completable
//import io.reactivex.Observable
//import io.reactivex.Single
//import io.reactivex.android.schedulers.AndroidSchedulers
//import io.reactivex.disposables.Disposable
//import io.reactivex.schedulers.Schedulers
//import me.gavin.R
//import me.gavin.util.log
//
//fun <T> Single<Result<T>>.httpCode() = this.checkResult().async().promptError()
//fun <T> Single<Result<T>>.httpData() = this.extractData().async().promptError()
//fun <T> Single<Result<List<T>>>.httpList() = this.extractList().async().promptError()
//fun <T> Single<Result<Pager<T>>>.httpPage() = this.extractPager().async().promptError()
//
//fun <T> Single<T>.async(): Single<T> = this
//        .subscribeOn(Schedulers.io())
//        .observeOn(AndroidSchedulers.mainThread())
//
//fun <T> Observable<T>.async(): Observable<T> = this
//        .subscribeOn(Schedulers.io())
//        .observeOn(AndroidSchedulers.mainThread())
//
//fun Completable.async(): Completable = this
//        .subscribeOn(Schedulers.io())
//        .observeOn(AndroidSchedulers.mainThread())
//
//fun <T> Single<T>.withProgress(progress: View, reverse: View? = null): Single<T> = this
//        .doOnSubscribe {
//            progress.visibility = View.VISIBLE
//            reverse?.let { it.visibility = View.INVISIBLE }
//        }
//        .doFinally {
//            progress.visibility = View.GONE
//            reverse?.let { it.visibility = View.VISIBLE }
//        }
//
//fun <T> Single<T>.withToolbarProgress(toolbar: Toolbar): Single<T> = this
//        .doOnSubscribe {
//            toolbar.menu?.findItem(R.id.actionLoading)?.isVisible = true
//        }.doFinally {
//            toolbar.menu?.findItem(R.id.actionLoading)?.isVisible = false
//        }
//
//fun SingleSubscribeProxy<*>.subscribeE(blk: ((Throwable) -> Unit)? = null): Disposable {
//    return this.subscribe { _, t ->
//        t?.also {
//            it.log(tag = "subscribeE")
//            blk?.invoke(it)
//        }
//    }
//}
//
//fun ObservableSubscribeProxy<*>.subscribeE(blk: ((Throwable) -> Unit)? = null): Disposable {
//    return this.subscribe({}, {
//        it.log(tag = "subscribeE")
//        blk?.invoke(it)
//    })
//}
//
//fun FlowableSubscribeProxy<*>.subscribeE(blk: ((Throwable) -> Unit)? = null): Disposable {
//    return this.subscribe({}, {
//        it.log(tag = "subscribeE")
//        blk?.invoke(it)
//    })
//}
//
//fun CompletableSubscribeProxy.subscribeE(blk: ((Throwable) -> Unit)? = null): Disposable {
//    return this.subscribe({}, {
//        it.log(tag = "subscribeE")
//        blk?.invoke(it)
//    })
//}
//
//fun <T> Single<Result<T>>.checkResult(): Single<Result<T>> = this
//        .map {
//            if (it.noLogin) {
////                UserHolder.clear()
////                if (checkState) {
////                    App.app.startActivity(Intent(App.app, LoginActivity::class.java)
////                            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
////                }
////                throw NoLoginException(it.code, it.info)
//            } else if (it.needCodeI) {
//                throw MissingCodeException()
//            } else if (!it.isSuccess) {
//                throw HttpResultException(it.code, it.info)
//            }
//            return@map it
//        }
//
//fun <T> Single<Result<T>>.extractData(): Single<T> = this
//        .checkResult()
//        .map {
//            it.data ?: throw EmptyDataException()
//        }
//
//fun <T> Single<Result<List<T>>>.extractList(): Single<List<T>> = this
//        .checkResult()
//        .map {
//            it.data.orEmpty()
//        }
//
//fun <T> Single<Result<Pager<T>>>.extractPager(): Single<List<T>> = this
//        .extractData()
//        .map {
//            it.data.orEmpty()
//        }
//
//fun <T> Single<T>.promptError(): Single<T> = this
//        .doOnError {
//            if (it is HttpResultException
//                    || it is NoLoginException) {
////                (it.message ?: "网络错误，请稍后再试").toastError()
//            } else if (it !is NoCacheException
//                    && it !is MissingCodeException) {
////                "网络错误，请稍后再试".toastError()
//            }
//        }
//        .subscribeOn(AndroidSchedulers.mainThread())
//
//fun <T> Observable<T>.promptError(): Observable<T> = this
//        .doOnError {
//            if (it is HttpResultException
//                    || it is NoLoginException) {
////                (it.message ?: "网络错误，请稍后再试").toastError()
//            } else if (it !is NoCacheException
//                    && it !is MissingCodeException) {
////                "网络错误，请稍后再试".toastError()
//            }
//        }
//        .subscribeOn(AndroidSchedulers.mainThread())
//
